import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import {config} from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-loadometer-page',
	templateUrl: './loadometer-page.component.html',
})
export class LoadometerPageComponent implements OnInit {
	validatorOptions = {
		excluded:[":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
		}
	};
	page: any = {
		carNumber:'',
		isDetail:  '',
		remark:  '',
		type:'',
		bridgeDate:  '',
	};
	page_title:string = '编辑';
	pageType:string = 'updata';
	recordId = null;
	curPage = 0;

	constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.recordId = queryParams.id;
			this.pageType = queryParams.type;
			this.curPage = queryParams.nowPage;
		});
	}
	//提交
	onSubmit(){
		let reqdata = 'id='+this.recordId+'&status='+this.page.isDetail+'&remark='+this.page.remark;
		this.jsonApiService.http("alertweigherror/editAlertForWeight.html",reqdata).subscribe(result=> {
			this.jsonApiService.message(result.message);
			if(result.success){
				this.goBack();
			}
		})
	}
	//返回资源列表页
	goBack() {
		this.router.navigate(['/abnormal-alert/loadometer'],{ queryParams: {nowPage:this.curPage}});
	}
	ngOnInit(){
		this.getEditData();
	}

	getEditData(){
		if(this.pageType === 'updata') {
			this.page_title = '编辑';
			let reqdata = 'id='+this.recordId;
			this.jsonApiService.http("alertweigherror/getAlertInfoById.html",reqdata).subscribe(data=> {
				if(data.success){
					data.data.isDetail=data.data.isDetail+'';
					for(let item in this.page){
						this.page[item] = data.data[item]?data.data[item]:'';
					}
					setTimeout(function(){
						$('#carNumberSelect2').val(this.page.deviceId).trigger('change');
						var str=$("<span class='select2-selection__rendered'>"+this.page.carNumber+"</span>");
						$('#carNumberSelect2').val("");
						$("#select2-carNumberSelect2-container").empty();
						$("#select2-carNumberSelect2-container").attr("title","");
						$("#select2-carNumberSelect2-container").prepend(str);
					}.bind(this),50);
				}else{
					this.jsonApiService.message(data.message);
				}
			})
		}
	}
}